package register;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.util.Arrays;
import java.util.List;

public class Register6703Controller {

    @FXML
    private TextField tfName;

    @FXML
    private PasswordField pf1;

    @FXML
    private PasswordField pf2;

    @FXML
    private TextField tfPhone;

    @FXML
    private Label lblName;

    @FXML
    private Label lblPW1;

    @FXML
    private Label lblPW2;

    @FXML
    private Label lblPhone;

    @FXML
    private Button btnOk;

    @FXML
    private ListView<User6703> lvUsers;

    @FXML
    void enter(ActionEvent event) {

        //回车判断当前选中的文本框
        Object source = event.getSource();
        if (source == tfName) {
            PassName();
        }
        if (source == pf1) {
            Passwords1();
        }
        if (source == pf2) {
            Passwords2();
        }
        if (source == tfPhone) {
            PassPhone();
        }
    }

    @FXML
    void Click(ActionEvent event) {
        //点击注册事件
        ObservableList<User6703> items = lvUsers.getItems();

        String name = tfName.getText();
        String pw2 = pf2.getText();
        String phone = tfPhone.getText();

        //如果全部输入均符合规范要求
        if (PassName() && Passwords1() && Passwords2() && PassPhone()) {
            items.add(new User6703(name, pw2, phone));
            alert("注册成功");
        }
    }

    //判断用户名是否是否唯一
    private boolean PassName() {
        ObservableList<User6703> items = lvUsers.getItems();
        String name = tfName.getText();

        for (int i = 0; i < items.size(); i++) {
            //遍历查找已注册用户列表检查

            //用户名必须唯一
            if (name.equals(items.get(i).getUsername()) || "".equals(name)) {
                lblName.setVisible(true);
                return false;
            } else {
                lblName.setVisible(false);
            }
        }
        return true;
    }

    //判断第一次输入密码格式规范
    private boolean Passwords1() {

        String pw1 = pf1.getText();

        //使用正则表达式检查
        //密码必须符合安全性：密码的长度不能低于6位，而且至少应该包含1个大写字母、1个小写字母、1个数字
        if (!pw1.matches("(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,}")) {
            lblPW1.setVisible(true);
            return false;
        } else {
            lblPW1.setVisible(false);
            return true;
        }
    }

    //判断第二次确认密码格式规范
    private boolean Passwords2() {
        String pw1 = pf1.getText();
        String pw2 = pf2.getText();
        if (
                //使用正则表达式检查
                //密码确认：输入两次相同的密码
                !pw2.equals(pw1) || !pw2.matches
                        ("(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,}"))
        {
            lblPW2.setVisible(true);
            return false;
        } else {
            lblPW2.setVisible(false);
            return true;
        }
    }

    //判断手机号码输入规范
    private boolean PassPhone() {
        String phone = tfPhone.getText();

        //使用正则表达式
        //手机号码是11位的数字，并且必须以13、15、17、18开头。
        if (!phone.matches("1[3578]\\d{9}") || "".equals(tfPhone.getText())) {
            lblPhone.setVisible(true);
            return false;
        } else {
            lblPhone.setVisible(false);
            return true;
        }
    }

    @FXML
    void initialize() {

        //初始化用户列表
        ObservableList<User6703> items = lvUsers.getItems();

        //已注册用户列表
        List<User6703> user6703s = Arrays.asList(
                new User6703("卞祖辉", "ABCabc123456", "13345678911"),
                new User6703("曾剑平", "ABCabc123456", "13345678922"),
                new User6703("陈杰", "ABCabc123456", "13345678933"),
                new User6703("陈亮宇", "ABCabc123456", "13345678944"),
                new User6703("小明", "ABCabc123456", "13345678955")
        );
        items.addAll(user6703s);

        //用户名监听输入框
        tfName.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                PassName();
            }
        });

        //第一次输入密码监听输入框
        pf1.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                Passwords1();
            }
        });

        //第二次输入密码监听输入框
        pf2.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                Passwords2();
            }
        });

        //手机号码监听输入框
        tfPhone.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                PassPhone();
            }
        });
    }

    //提示窗口方法
    void alert(String al) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("用户注册V1.0｜集合版");
        alert.setHeaderText("提示");
        alert.setContentText(al);
        alert.show();
    }
}